package com.ftg.learn.day11;

import java.io.*;

/**
 * 图片管理
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/17 下午1:46
 */
public class Imgio {

    public static void main(String[] args) {
        File f1 = new File("/home/kangjx/Desktop/aa.png");


        System.out.printf("文件名称%s\n", f1.getName());
        System.out.printf("文件名称%d\n", f1.lastModified());
        System.out.printf("隐藏文件%b\n", f1.isHidden());
        System.out.printf("是否为文件%b\n", f1.isFile());
        System.out.printf("是否为文件%b\n", f1.isDirectory());
        System.out.printf("是否可读%b\n", f1.canRead());

//        FileInputStream fio = null;
//        try {
//            fio = new FileInputStream(f1);
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (null != fio) {
//                try {
//                    fio.close();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }

        //第二种写法 try with resources

        File f2 = new File("/home/kangjx/Desktop/bb.png");
        if(f2.exists()){
            try {
                f2.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try (
                //resources
                FileInputStream fio1 = new FileInputStream(f1);
                FileOutputStream fs = new FileOutputStream(f2);
        ) {
            int n = 0;
            while ((n = fio1.read()) != -1) {
                fs.write(n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
